/**
 * @Description: commit信息问答器
 * @author Beon
 * @date 2021/1/14
*/
import {types} from '../../json/commitType.json';
import * as branchType from '../../json/branchType.json';
import * as map from 'lodash.map';
import * as inquirer from 'inquirer';
import {QuestionCollection} from 'inquirer';
import * as longest from 'longest';
import {OraLoading} from '../decorators/oraLoading';

// 处理对象参数为提问格式
const length = longest(Object.keys(types)).length + 1;
const COMMITTYPE = map(types, function(type, key) {
    return {
        name: (key + ':').padEnd(length) + ' ' + type.description,
        value: key
    };
});

const bLength = longest(Object.keys(branchType.types)).length + 1;
const BRANCHTYPE = map(branchType.types, function(type, key) {
    return {
        name: (key + ':').padEnd(bLength) + ' ' + type.description,
        value: key
    };
});

export interface backCommitData {
    type: string,
    body: string
}

export type backBranchTypeData = string

export class CommitBranchInquirer {
    @OraLoading.beProxy
    static status: OraLoading;
    private static commitType: {
        name: string
        value: string
    }[] = COMMITTYPE
    private static typeInquirer = {
        type: 'list',
        name: 'type',
        message: '选择你的commit类型',
        choices: CommitBranchInquirer.commitType
    }
    private static longerInquirer = {
        type: 'input',
        name: 'body',
        message: '填写您的提交描述(可不填)：\n',
    }
    private static branchType: {
        name: string
        value: string
    }[] = BRANCHTYPE
    private static branchInquirer = {
        type: 'list',
        name: 'type',
        message: '选择你将要进行的任务类型',
        choices: CommitBranchInquirer.branchType
    }

    /**
     * 执行获取commit信息
     * @returns {Promise<backCommitData>}
     */
    static async getCommitInquirer(): Promise<backCommitData> {
        this.status.oraLoadingStop();

        const BACKDATA: backCommitData = await this.inquirerRun([this.typeInquirer, this.longerInquirer]);

        this.status.oraLoadingStart();

        return BACKDATA;
    }

    /**
     * 获取创建分支类型
     * @returns {Promise<string>}
     */
    static async getBranchTypeInquirer(): Promise<string> {
        this.status.oraLoadingStop();

        const BACKDATA: {type: backBranchTypeData} = await this.inquirerRun([this.branchInquirer]);

        this.status.oraLoadingStart();

        return BACKDATA.type;
    }

    /**
     * 获取详细描述
     * @returns {Promise<string>}
     */
    static async getBodyInquirer(): Promise<string> {
        this.status.oraLoadingStop();

        const BACKDATA: {body: string} = await this.inquirerRun([this.longerInquirer]);

        this.status.oraLoadingStart();

        return BACKDATA.body;
    }

    /**
     * 执行提问器
     * @param inquirerObj
     * @returns {Promise<Promise<any> & {ui: PromptUI}>}
     */
    private static async inquirerRun<T>(inquirerObj: QuestionCollection<T>):Promise<T> {
        return inquirer
            .prompt(inquirerObj);
    }
}
